package com.day1;

import com.alibaba.fastjson.JSON;
import hk.JdbcUtil;
import com.entity1.MyUser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/entity")
public class User extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        //解决请求与响应乱码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取请求参数
        String key = req.getParameter("key");

        //把参数作为查询条件去数据库查询
        JdbcUtil jdbcUtil=new JdbcUtil();
        try {
            MyUser user = jdbcUtil.findUser(key);
            //把查出来的数据返回响应
            //System.out.println(entity);
            String s = JSON.toJSONString(user);
            resp.getWriter().print(s);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
